using UnityEngine;

namespace UnityTouchTable.Example
{
    /// <summary>
    /// 图片子内容节点
    /// </summary>
    [RequireComponent(typeof(SpriteRenderer))]
    public class TextureContentNode : SubContentNodeBase
    {
        #region Field
        /// <summary>
        /// 精灵渲染器
        /// </summary>
        [SerializeField]
        [Header("精灵渲染器")]
        private SpriteRenderer m_SpriteRenderer = default;

        /// <summary>
        /// 精灵集
        /// </summary>
        [SerializeField]
        [Header("精灵集")]
        private Sprite[] m_Sprites = default;

        /// <summary>
        /// 循环切换
        /// </summary>
        [SerializeField]
        [Header("循环切换")]
        private bool m_Loop = false;

        /// <summary>
        /// 当前精灵索引
        /// </summary>
        private int m_SpriteIndex = 0;
        #endregion

        #region Function
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="centerRadius">中心节点半径</param>
        /// <param name="angle">角度</param>
        public override void Show(float centerRadius, float angle = 0.0f)
        {
            base.Show(centerRadius, angle);

            this.m_SpriteIndex = 0;
            this.m_SpriteRenderer.sprite = this.m_Sprites[this.m_SpriteIndex];

            //TODO:按钮注册
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        /// <param name="exit">mark退出</param>
        public override void Hide(bool exit = false)
        {
            base.Hide(exit);

            //TODO:按钮取消注册
        }

        /// <summary>
        /// 左切换
        /// </summary>
        private void LeftSwitchSprite()
        {
            this.m_SpriteIndex--;
            if (this.m_Loop)
            {
                if (this.m_SpriteIndex < 0)
                    this.m_SpriteIndex = this.m_Sprites.Length;
            }
            else
            {
                //逻辑上不会小于0
                if (this.m_SpriteIndex < 0)
                {
                    this.m_SpriteIndex = 0;
                    return;
                }
                else if (this.m_SpriteIndex == 0)
                {
                    this.m_SpriteIndex = 0;
                    //TODO:禁用left按钮
                }
                else
                {
                    //TODO:启用left按钮
                }
            }
            this.m_SpriteRenderer.sprite = this.m_Sprites[this.m_SpriteIndex];
        }

        /// <summary>
        /// 右切换
        /// </summary>
        private void RightSwitchSprite()
        {
            this.m_SpriteIndex++;
            if (this.m_Loop)
            {
                if (this.m_SpriteIndex >= this.m_Sprites.Length)
                    this.m_SpriteIndex = 0;
            }
            else
            {
                //逻辑上不会大于等于精灵集数量
                if (this.m_SpriteIndex >= this.m_Sprites.Length)
                {
                    this.m_SpriteIndex = this.m_Sprites.Length - 1;
                    return;
                }
                else if (this.m_SpriteIndex == this.m_Sprites.Length - 1)
                {
                    //TODO:禁用right按钮
                }
                else
                {
                    //TODO:启用right按钮
                }
            }
            this.m_SpriteRenderer.sprite = this.m_Sprites[this.m_SpriteIndex];
        }
        #endregion
    }
}